
/***  DOCUMENTATION LAYER

	Javascript Function Library Template

	Last Update: Mar 2007
	Author: Tom at klenwell@gmail.com
	License: GPL License

FUNCTIONS	
	js_load_script_array(JS_FILES)
	rand(min, max)
	div_write(inner_html, div_style, div_id, div_class)

NOTES
	To link: <script type="text/javascript" src="path/to/file.js"></script>

REFERENCE
	http://developer.mozilla.org/en/docs/JavaScript
	LGPL License: http://www.opensource.org/licenses/lgpl-license.php
	GPL License: http://www.opensource.org/licenses/gpl-license.php

______________________________________________________________________________*/


// js_load_script_array
/*
	this function will automatically load any files in an array based on the path
	of the last script tag -- useful at the beginning of a js library or class file
	to autoload any dependant files
	
	Caution: must best used in external files since it uses last script tag src value 
	as a reference (which would be null for internal script tags)
*/
/*____________________________________________________________________________*/
function js_load_script_array(JS_FILES)
{
	// Dynamically determine directory root path
	var ObjScript = document.getElementsByTagName("script");
	var src_idx_n = ObjScript.length-1;	/* grab source for last script tag */
	var js_src = ObjScript[src_idx_n].getAttribute("src");
	var fname_offset = js_src.lastIndexOf ( '/' ); 
	var dirpath = ( fname_offset >= 0 ? js_src.substring ( 0, fname_offset + 1 ) : "" );
	if ( 0 ) alert(dirpath);

	// Load files
	for ( var i=0; i < JS_FILES.length; i++ )
	{
		var new_src = dirpath + JS_FILES[i];
		var script_tag = '<script type="text/javascript" src="' + new_src + '"></script>';
		if ( 0 ) alert(script_tag);
		document.write(script_tag); 
	}
	
	return;
	
} // end Fx
/*____________________________________________________________________________*/


// rand
// source: http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Math:random#Example:_Using_Math.random
/*____________________________________________________________________________*/
function rand(min, max)
{
	return Math.floor(Math.random() * (max - min + 1) + min);
}
/*____________________________________________________________________________*/


// div_write 
/*____________________________________________________________________________*/
function div_write(inner_html, div_style, div_id, div_class)
{
	// declarations
	var style_att = '';
	var id_att = '';	
	var class_att = '';

	// initialize parameters
	if ( div_style != null ) style_att = ' style="'+ div_style +'"';
	if ( div_id != null ) id_att = ' id="'+ div_id +'"';
	if ( div_class != null ) class_att = ' class="'+ div_class +'"';

	document.write('<div' + id_att + class_att + style_att + '">' + inner_html + '</div>');
	return;
}
/*____________________________________________________________________________*/


// url_redirect 
/*____________________________________________________________________________*/
function url_redirect(url)
{
	window.location = url;
	return;
}
/*____________________________________________________________________________*/

